package com.chujian.framework.core.base;

import com.baomidou.mybatisplus.annotation.TableField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.experimental.Accessors;

import java.util.ArrayList;
import java.util.List;

/**
 * 树结构Entity约束
 * <E> element
 *
 * @author chujian
 * @since 2021-12-11
 */
@Getter
@Setter
@Accessors(chain = true)
@ToString(callSuper = true)
public abstract class BaseTreeEntity<E> extends BaseEntity {

    @ApiModelProperty(value = "父ID")
    @TableField(value = "parent_id")
    protected Long parentId;

    @ApiModelProperty(value = "排序号")
    @TableField(value = "order_no")
    protected Integer orderNo;

    @ApiModelProperty(value = "子节点", hidden = true)
    @TableField(exist = false)
    protected List<E> children;

    @JsonIgnore
    public void initChildren() {
        if (null == getChildren()) {
            this.setChildren(new ArrayList<>());
        }
    }

    @JsonIgnore
    public void addChildren(E child) {
        initChildren();
        children.add(child);
    }

}
